package com.me.bbs.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.common.AdminType;
import com.me.bbs.common.AuthUtils;
import com.me.bbs.common.SessionUtils;
import com.me.bbs.dto.AdminDTO;
import com.me.bbs.dto.UserDTO;
import com.me.bbs.entity.Administrator;
import com.me.bbs.entity.Forum;
import com.me.bbs.entity.User;
import com.me.bbs.mapper.AdministratorMapper;
import com.me.bbs.service.IAdministratorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.me.bbs.service.IForumService;
import com.me.bbs.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lm
 * @since 2023-01-29
 */
@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator> implements IAdministratorService {

    @Resource
    private IUserService userService;
    @Resource
    private IForumService forumService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public    Page<AdminDTO> searchUser(Long forumId, String q, Long current, Long size) {
        Forum f = forumService.lambdaQuery().eq(Forum::getId, forumId).one();
        Long forumOwnerId = f.getOwner();
        Page<User> userPage = userService.lambdaQuery().like(User::getNickname, q).or().like(User::getUsername, q)
                .notIn(User::getUserId, forumOwnerId)
                .page(new Page<>(current,size));
        List<User> userList = userPage.getRecords();
        List<AdminDTO> adminDTOS = new ArrayList<>(userList.size());
        userList.forEach(u->{
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(u, userDTO);
            AdminDTO adminDTO = new AdminDTO();
            adminDTO.setUser(userDTO);
            adminDTO.setForum(forumId);
            Administrator adminE = this.lambdaQuery().eq(Administrator::getUser, userDTO.getUserId())
                    .eq(Administrator::getForum, forumId)
                    .one();
            if(adminE == null) adminDTO.setIsAdmin(false);
            else {
                adminDTO.setId(adminE.getId());
                adminDTO.setCreateAt(adminE.getCreateAt());
                adminDTO.setIsAdmin(true);
            }
            adminDTOS.add(adminDTO);
        });
        Page<AdminDTO> adminDTOPage = new Page<>();
        BeanUtils.copyProperties(userPage, adminDTOPage);
        adminDTOPage.setRecords(adminDTOS);
        return adminDTOPage;
    }

    @Override
    public void addAdmin(Long forumId, Long userId) {
//        Long uid = (Long) SessionUtils.getAttribute("user");
        AuthUtils.checkAdmin(AdminType.OWNER, forumId);
        Administrator administrator = new Administrator();
        administrator.setForum(forumId);
        administrator.setUser(userId);
        this.save(administrator);
    }

    @Override
    public Page<AdminDTO> getAdmins(Long forumId, Long current, Long size) {
        Page<Administrator> adminEPage = this.lambdaQuery().eq(Administrator::getForum, forumId)
                .page(new Page<>(current, size));
        List<Administrator> records = adminEPage.getRecords();
        List<AdminDTO> adminDTOS = new ArrayList<>(records.size());
        records.forEach(a -> {
            AdminDTO adminDTO = new AdminDTO();
            BeanUtils.copyProperties(a, adminDTO);
            User user = userService.lambdaQuery().eq(User::getUserId, a.getUser()).one();
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            adminDTO.setUser(userDTO);
            adminDTO.setIsAdmin(true);
            adminDTOS.add(adminDTO);
        });
        Page<AdminDTO> page = new Page<>();
        BeanUtils.copyProperties(adminEPage, page);
        page.setRecords(adminDTOS);
        return page;
    }

    /**
    * @date: 2023/2/3 21:29
    * @author: Liming Wang
    * @return String 返回权限信息 owner:吧主，admin：管理员,user:普通用户
    * @since JDK 1.8
    */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getInfo(Long forumId) {
        Long uid = (Long) SessionUtils.getAttribute("user");
        String state = "";
        boolean isAdmin = this.lambdaQuery().eq(Administrator::getUser, uid).eq(Administrator::getForum, forumId)
                .exists();
        if(isAdmin)
            state = "admin";
        boolean isOwner = forumService.lambdaQuery().eq(Forum::getId, forumId).eq(Forum::getOwner, uid).exists();
        if(isOwner)
            state = "owner";
        if(!(isAdmin || isOwner))
            state = "user";
        return state;
    }
}
